package com.hongdee.scm.provideCompany.service.impl;

import com.hongdee.common.dao.IHibernateDao;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.scm.provideCompany.dao.ITScmProvideCompanyDao;
import com.hongdee.scm.provideCompany.entity.TScmProvideCompany;
import com.hongdee.scm.provideCompany.service.ITScmProvideCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2017/9/15.
 */
@Service
public class TScmProvideCompanyServiceImpl implements ITScmProvideCompanyService{

    @Autowired
    private ITScmProvideCompanyDao itScmProvideCompanyDao;
    @Autowired
    private IHibernateDao ihibernateDao;

    /**
     * 新增或编辑
     * @param data
     * @param uid
     * @throws Exception
     */
    public void saveOrUpdate(TScmProvideCompany data, String uid) throws Exception{

        String cid = TokenUtil.getCompanyId(uid);
        if(data.getId()==null){ //新增

            //默认启用
            data.setStatus("1");
            data.setCompanyId(cid);
            data.setCreateUser(uid);
            data.setCreateTime(Calendar.getInstance().getTimeInMillis());
            this.itScmProvideCompanyDao.saveOrUpdate(data);

        }else{

            data.setUpdateUser(uid);
            data.setUpdateTime(Calendar.getInstance().getTimeInMillis());
            this.itScmProvideCompanyDao.saveOrUpdate(data);
        }
    }

    /**
     * 单条查询
     * @param id
     * @param uid
     * @return
     * @throws Exception
     */
    public Map info(String id, String uid) throws Exception{

        if(id==null||id.equals("")){
            throw new Exception("请选择数据");
        }
        List params = new ArrayList();
        String sql = "select * from t_scm_providecompany where id = ? and company_id= ?";
        params.add(id);
        params.add(TokenUtil.getCompanyId(uid));
        return this.itScmProvideCompanyDao.info(sql,params.toArray());
    }

    /**
     * 启用禁用
     * @param pcid
     * @param status
     * @param uid
     * @throws Exception
     */
    public void startOrStop (String pcid,String status,String uid) throws Exception{

        if(pcid==null||pcid.equals("")){
            throw new Exception("请选择数据");
        }
        String hql = "update TScmProvideCompany set status=:status where id = :id and companyId=:companyId";
        Map params = new HashMap();
        params.put("status", status);
        params.put("id", pcid);
        params.put("companyId",TokenUtil.getCompanyId(uid));
        this.itScmProvideCompanyDao.update(hql, params);
    }

    /**
     * 查询
     * @param data
     * @param uid
     * @param start
     * @param limit
     * @return
     * @throws Exception
     */
    public Map queryCompany(Map data,String uid,String start,String limit) throws Exception{

        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        sql.append("select p.* from t_scm_providecompany p where p.company_id=?");
        params.add(TokenUtil.getCompanyId(uid));
        //客商类型(0供货商 1销货)
        if(data.get("customerType")!=null&&!data.get("customerType").equals("")){
          sql.append(" and p.customer_type = ?");
          params.add(data.get("customerType"));
        }
        //客户分类(0厂商1经销商2批发3零售)
        if(data.get("customerClassify")!=null&&!data.get("customerClassify").equals("")){
            sql.append(" and p.customer_classify = ?");
            params.add(data.get("customerClassify"));
        }
        sql.append(" order by p.customer_name");
        Map map = this.itScmProvideCompanyDao.page(sql.toString(),params.toArray(),start,limit);
        return map;
    }

    /**
     * 下拉列表
     * @param uid
     * @param type
     * @return
     * @throws Exception
     */
    public List<Map> combo(String uid,String type) throws Exception{

        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        sql.append("select p.* from t_scm_providecompany p where p.company_id=?");
        params.add(TokenUtil.getCompanyId(uid));
        //客商类型(0供货商 1销货)
        if(type!=null&&!type.equals("")){
            sql.append(" and p.customer_type=?");
            params.add(type);
        }
        sql.append(" order by p.customer_name");
        List list = this.itScmProvideCompanyDao.list(sql.toString(),params.toArray());
        return list;
    }
}
